package pl.th.permission.demo.example;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.io.File;
import java.io.RandomAccessFile;

import pl.th.permission.annotation.NeedPermission;
import pl.th.permission.annotation.OnAllPermissionsDenied;
import pl.th.permission.annotation.OnAllPermissionsGranted;
import pl.th.permission.annotation.OnDeniedCallback;
import pl.th.permission.annotation.OnGrantedCallback;
import pl.th.permission.demo.R;

public class CallbackActivity extends BaseActivity{

    private static final String TAG = "CallbackActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_callback);
        findViewById(R.id.location).setOnClickListener(v -> {
            requestLocationPermission();
        });
        findViewById(R.id.storage).setOnClickListener(v -> {
            requestStoragePermission();
        });
        findViewById(R.id.camera).setOnClickListener(v -> {
            requestCameraPermission();
        });
    }

    @NeedPermission(
            requestCode = 100,
            permissions = {
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.MANAGE_EXTERNAL_STORAGE
            }
    )
    private void requestStoragePermission() {

    }

    @NeedPermission(
            requestCode = 200,
            permissions = {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.MANAGE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
            }
    )
    private void requestCameraPermission() {

    }

    @NeedPermission(
            requestCode = 300,
            permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION
            }
    )
    private void requestLocationPermission() {

    }

    @OnGrantedCallback
    private void afterPermissionGranted(int requestCode, String[] permissions) {
        final String text = "granted: requestCode = " + requestCode + ", permissions = " + join(permissions);
        Log.i(TAG, text);
        toast(text);
    }

    @OnDeniedCallback
    private void afterPermissionDenied(int requestCode, String[] permissions) {
        final String text = "denied: requestCode = " + requestCode + ", permissions = " + join(permissions);
        Log.w(TAG, text);
        toast(text);
    }

}
